package com.veer.shop.service.impl.admin;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.veer.shop.config.ResponseCodeEnum;
import com.veer.shop.mapper.ProductTagMapper;
import com.veer.shop.param.TagParam;
import com.veer.shop.pojo.ProductTag;
import com.veer.shop.pojo.ResponseData;
import com.veer.shop.pojo.ValidResult;
import com.veer.shop.service.admin.TagService;
import com.veer.shop.utils.DateTimeUtil;
import com.veer.shop.utils.InitUtil;
import com.veer.shop.utils.ResponseUtil;

/**
 * 商品品牌业务实现类
 * 
 * @author Wangbin
 * 2020年9月22日-上午9:45:59
 */
@Service
public class TagServiceImpl implements TagService {
	
	@Autowired
	ProductTagMapper productTagMapper;

	/**
	 * 新增|更新标签
	 */
	@Override
	public ResponseData<?> update(ProductTag tag) {
		// TODO Auto-generated method stub
		// 校验参数
		ValidResult verifyResult = verifyTag(tag);
		int count = 0;
		if(verifyResult.getCode() == 0) {
			if(tag.getTagId() > 0) {
				// 更新品牌
				count = productTagMapper.update(tag);
			} else {
				// 新增品牌
				tag.setCreatedAt(DateTimeUtil.getDateTime());
				tag.setUpdatedAt(tag.getCreatedAt());
				
				count = productTagMapper.insert(tag);
			}
			
			if(count > 0) {
				return ResponseUtil.success();
			} else {
				return ResponseUtil.error();
			}
		} else {
			return ResponseUtil.error(verifyResult.getMessage());
		}
	}

	/**
	 * 标签列表
	 */
	@Override
	public ResponseData<?> getAll(TagParam tagListParam, Integer pageNo, Integer pageSize) {
		// TODO Auto-generated method stub
		Map<String, Object> listResultMap = InitUtil.initListResultMap();
		Page page = PageMethod.startPage(pageNo, pageSize);
		List<Map<String, Object>> brands = productTagMapper.selectAll(tagListParam);
		listResultMap.put("count", page.getTotal());
		
		listResultMap.put("list", brands);
		return ResponseUtil.success(listResultMap);
	}

	/**
	 * 删除标签
	 */
	@Override
	public ResponseData<?> delete(String tagId) {
		
		// 处理品牌ID
		String[] tagIdList = tagId.split(",");
		
		for(String id : tagIdList) {
			
			int tid = Integer.parseInt(id);
			
			// 查询品牌
			Map<String, Object> findTag = productTagMapper.selectById(tid);
			if(findTag == null)continue;
			
			productTagMapper.delete(tid);
		}
		
		return ResponseUtil.success();
	}

	/**
	 * 品牌详情
	 */
	@Override
	public ResponseData<?> get(Integer tagId) {
		// 查询品牌
		Map<String, Object> findBrand = productTagMapper.selectById(tagId);
		
		if(findBrand == null)return ResponseUtil.error(ResponseCodeEnum.VIEW_PRODUCT_BRAND_ERROR.getMessage());
		
		return ResponseUtil.success(findBrand);
	}

	/**
	 * 校验标签参数合法性
	 * 
	 * @param tag
	 * @return
	 */
	private ValidResult verifyTag(ProductTag tag) {
		
		ValidResult result = InitUtil.initFaildMap();
		
		if(tag.getTagName().equals("")) {
			result.setMessage("标签名称不可为空！");
			return result;
		}
		
		if(tag.getTagId() > 0) {
			// 查询品牌
			Map<String, Object> findTag = productTagMapper.selectById(tag.getTagId());
			
			if(findTag == null) {
				result.setMessage("标签不存在！");
				return result;
			}
		}
		
		int count = productTagMapper.search(tag.getTagName(), tag.getTagId());
		
		if(count > 0) {
			result.setMessage("名称已存在！");
			return result;
		}
		
		return InitUtil.initSuccMap();
	}
}
